package com.binc.testspring.common.excel;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.OutputStream;
import java.util.List;

public class EasyExcelWriteUtils {

    public final static int colSplit = 0, rowSplit = 1, leftmostColumn = 0, topRow = 1;
    public static final String AUTOFILTERRANGE = "1:1";

    // XLSX  --> Excel2007及以后的版本最大行数是1048576行，最大列数是16384列

    /**
     * 需要导出多个sheet时  需要手动关流
     *
     * @param out
     * @param clz
     * @return
     */
    public static <T> ExcelWriterBuilder getExcelWriterBuilder(OutputStream out, Class<T> clz) {
        return EasyExcelFactory.write(out, clz)
                .autoCloseStream(true)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(getHorizontalCellStyleStrategy()) //自定义了一些字体、背景色、下划线等
                .registerWriteHandler(new MyMergeStrategy());//自适应列宽
    }

    /**
     * 需要导出多个sheet时  需要手动关流
     *
     * @param out
     * @return
     */
    public static <T> ExcelWriterBuilder getExcelWriterBuilder(OutputStream out) {
//        return EasyExcelFactory.write(out).registerWriteHandler(getHorizontalCellStyleStrategy()).registerWriteHandler(getFreezeHandler());
        return EasyExcelFactory.write(out).autoCloseStream(true).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(getHorizontalCellStyleStrategy());

    }

    /**
     * 导出单sheet  无需手动finish
     *
     * @param out
     * @param clz
     * @param sheetName
     * @param data
     */
    public static <T> void customTitleWriter(OutputStream out, Class<T> clz, String sheetName, List<T> data) {
        EasyExcel.write(out, clz).autoCloseStream(true).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(getHorizontalCellStyleStrategy()).sheet(sheetName).doWrite(data);
    }

    public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为红色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 11);
        headWriteFont.setFontName("微软雅黑");
        headWriteFont.setBold(true);

        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setBorderBottom(BorderStyle.THICK);
        headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        headWriteCellStyle.setBorderRight(BorderStyle.THIN);
        headWriteCellStyle.setBorderTop(BorderStyle.THICK);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 10);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }

    /**
     * 锁定表头
     *
     * @return
     */
    private static WriteHandler getFreezeHandler() {
        return FreezeHandler.FREEZE_HANDLER;
    }

    private static class FreezeHandler implements SheetWriteHandler {

        public static final WriteHandler FREEZE_HANDLER = new FreezeHandler();

        @Override
        public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

        }

        @Override
        public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
            Sheet sheet = writeSheetHolder.getSheet();
            sheet.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
            sheet.setAutoFilter(CellRangeAddress.valueOf(AUTOFILTERRANGE));
        }
    }

}